package Dao.Impl;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import Dao.UserDao;
import Form.User;


public class UserDaoImpl implements UserDao {
	
	protected final Log log = LogFactory.getLog(getClass());
	
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public int insertUsesUpdate(User user) {
		String sql = "insert into logserve_user values(?,?)";
		Object[] params = new Object[] { user.getName(), user.getPassword() };
		return this.getJdbcTemplate().update(sql, params);
	}

	public int insertPersonUseExecute(User user) {
		String sql = "insert into logserve_user values(?,?)";
		Object[] params = new Object[] { user.getName(), user.getPassword() };
		int[] types = new int[] { Types.VARCHAR, Types.VARCHAR };
		return this.getJdbcTemplate().update(sql, params, types);
	}

	public int[] updatePersonUseBatchUpdate(final List<User> users) {
		String sql = "insert into logserve_user values(?,?)";
		BatchPreparedStatementSetter setter = null;
		setter = new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return users.size();
			}

			public void setValues(PreparedStatement ps, int index)
					throws SQLException {
				User user = (User) users.get(index);
				ps.setString(1, user.getName());
				ps.setString(2, user.getPassword());
			}
		};
		return this.getJdbcTemplate().batchUpdate(sql, setter);
	}

	public User getUserByUsername(String username) {

		String sql = "SELECT user_id, username, password, name, sex, age, tel, birthday, mail, power, state FROM logserve_user where username=?";
		Object params[] = new Object[] { username };
		List<?> matches = jdbcTemplate.query(sql, params, new RowMapper() {
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException,
					DataAccessException {
				User user = new User();
				user.setUserId(rs.getString(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setName(rs.getString(4));
				user.setSex(rs.getInt(5));
				user.setAge(rs.getInt(6));
				user.setTel(rs.getString(7));
				user.setBirthday(rs.getString(8));
				user.setMail(rs.getString(9));
				user.setPower(rs.getInt(10));
				user.setState(rs.getInt(11));
				return user;
			}

		});
		return matches.size() > 0 ? (User) matches.get(0) : null;
	}
	/*
	 * 查询MAX USERID
	 */
	public int maxUserId(){
		String sql = " SELECT  user_id FROM logserve_user  ORDER BY user_id DESC LIMIT 1;";
		return this.jdbcTemplate.queryForInt(sql);
	}
	/*
	 * 用户注册
	 */
	public int insertUser(User user) {
		int userId = maxUserId()+1;
		user.setUserId(Integer.toString(userId));
		user.setPower(2);
		user.setState(0);
		String sql = "INSERT INTO logserve_user(user_id, username, password, " +
				"name, sex, age, tel, birthday, mail, power, state) " +
				"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
		Object[] params = new Object[] { user.getUserId(), user.getName(), user.getPassword(), 
				user.getName(), user.getSex(), user.getAge(), user.getTel(), user.getBirthday(), 
				user.getMail(), user.getPower(), user.getState()};
		int[] types = new int[] { Types.VARCHAR, Types.VARCHAR ,Types.VARCHAR,
				Types.VARCHAR,Types.INTEGER,Types.INTEGER,Types.VARCHAR,Types.VARCHAR,
				Types.VARCHAR,Types.INTEGER,Types.INTEGER};
		
		if (log.isDebugEnabled()){
			log.debug("sql=" + sql + "/param=" + params);
		}
		return this.getJdbcTemplate().update(sql, params, types);
	}
}